#include "BackgroundLoader.h"

using Magick::Image;
using Magick::InitializeMagick;

namespace backgrounds{
	GLuint BackgroundLoader::textures[2];

	BackgroundLoader::BackgroundLoader(){}

	BackgroundLoader::BackgroundLoader(float _height, float _width, float _depth, bool _planeSelector){
		planeSelector = _planeSelector;
		height = _height;
		width  = _width;
		depth  = _depth;

		glColor3f(1.0f, 1.0f, 1.0f);
		if(!glIsTexture(textures[1]) && !glIsTexture(textures[2])){
			//prepare the texture array
			glGenTextures(2, textures);
		}
		currentTexture = 0;
		setTexture("resources/textures/cityscape2.jpg");

		currentTexture = 1;
		setTexture("resources/textures/space2.jpg");

		glEnable(GL_TEXTURE_2D);
		if(!planeSelector){
			glTranslatef(0.0f, 0.0f, -5.0f);
			glScalef(width*2.5f, height*2.5f, 0.0f);
			renderBackPlane();
		} else {
			glTranslatef(0.0f, 30, -5.0f);
			glScalef(width*34, 0.0f, depth*50);
			renderTopPlane();
		}
		glDisable(GL_TEXTURE_2D);
	}

	BackgroundLoader::~BackgroundLoader(){
		glDeleteTextures(2, textures);
	}

	void BackgroundLoader::setTexture(char *fileName){

		if(glIsTexture(textures[currentTexture])) {return;}

		glPixelStorei(GL_UNPACK_ALIGNMENT, textures[currentTexture]);

		glBindTexture(GL_TEXTURE_2D, textures[currentTexture]);

		Image image;
		image.read(fileName);

		image.rotate(180.0f);
		glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
		glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
		glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);

		glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, image.columns(), image.rows(), 0,
			GL_BGRA_EXT,
			GL_UNSIGNED_SHORT,
			image.getPixels(0, 0, image.columns(), image.rows()));

		glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
		glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
	}

	void BackgroundLoader::setTexture(char *fileName, int location){

		glPixelStorei(GL_UNPACK_ALIGNMENT, textures[location]);

		glBindTexture(GL_TEXTURE_2D, textures[location]);

		Image image;
		image.read(fileName);

		//image.flip();
		image.rotate(180.0f);
		glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
		glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
		glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);

		glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, image.columns(), image.rows(), 0,
			GL_BGRA_EXT,
			GL_UNSIGNED_SHORT,
			image.getPixels(0, 0, image.columns(), image.rows()));

		glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
		glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
	}

	void BackgroundLoader::renderBackPlane(){
		glBindTexture(GL_TEXTURE_2D, textures[0]);
		glBegin(GL_QUADS);
		glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, -1.0f);	// Bottom Right Of The Texture and Quad
		glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f,  1.0f, -1.0f);	// Top Right Of The Texture and Quad
		glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f,  1.0f, -1.0f);	// Top Left Of The Texture and Quad
		glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f, -1.0f);	// Bottom Left Of The Texture and Quad
		glEnd();

	}

	void BackgroundLoader::renderTopPlane(){
		glBindTexture(GL_TEXTURE_2D, textures[1]);
		//glColor3f(0.0f, 0.0f, 0.0f);
		glBegin(GL_QUADS);
		glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f,  1.0f, -1.0f);	// Top Left Of The Texture and Quad
		glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f,  1.0f,  1.0f);	// Bottom Left Of The Texture and Quad
		glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f,  1.0f,  1.0f);	// Bottom Right Of The Texture and Quad
		glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f,  1.0f, -1.0f);	// Top Right Of The Texture and Quad
		glEnd();
	}
	
}